# coding=utf-8

import web

import utils

def get_last_actress(limit = 10):
    '获取最新的猫优（默认10条）'
    return web.ctx.db.select('actress'
            , order = 'id DESC'
            , limit = limit)

# @uid 当前登录用户
# @ipt 客户端表单提交上来的数据，由web.inputs()方法获取
def add_actress_approving_queued(ipt, uid = 0):
    '添加一条推荐猫优信息到审核队列中'
    # 当前用户是否推荐过该用户
    if not is_exists_actress_approving_queued(uid, ipt.url):
        # 当前用户没有推荐过
        return web.ctx.db.insert('actress_approving_queue'
                , user_id = uid
                , name = utils.html_clear(ipt.name)
                , url = ipt.url
                , description = utils.html_clear(ipt.fields)
                , rss = ''
                , date_created = web.SQLLiteral('NOW()')
                , status = 0
                , abstract = ''
                , ip = web.ctx.ip)
    return False

# @uid 提交推荐的用户id
# @url 提交推荐的博客地址
def is_exists_actress_approving_queued(uid, url):
    '判定给定URL是否已经被推荐过了'
    sets = web.ctx.db.query('''SELECT EXISTS(SELECT NULL 
                                             FROM actress_approving_queue 
                                             WHERE user_id = $uid 
                                                    AND url = $url) AS value;'''
                            , vars = locals())
    if sets:
        return sets[0].value == 1
    return False
